//
//  GIFViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/17.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "GIFViewController.h"
#import "GifCell.h"
#import "GifModel.h"
#import "AfNetWorkingHandler.h"
#import <MJRefresh.h>
#import "MBProgressHUDManager.h"
#import "BlackManager.h"
#import "SetBlack.h"
@interface GIFViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)NSInteger number;
@end

@implementation GIFViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    _tableView.bounces = NO;
    //_tableView.backgroundColor = [UIColor cyanColor];
    [_tableView registerClass:[GifCell class] forCellReuseIdentifier:@"cellG"];
    [self.view addSubview:_tableView];
    [_tableView release];
    self.navigationItem.title = @"搞笑动图";
    //数据处理
    [self dataHandler];
//    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    [_tableView.mj_header beginRefreshing];
    
    // 上拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_dataArr.count != 0) {
        [_tableView.mj_footer beginRefreshing];
    }
    self.number = 2;
    
    
    //隐藏上下边栏
    //是否实现了方法
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;//取消scrollview的内容自动调整
        UIEdgeInsets insets = self.tableView.contentInset;//tableView边界预留
        insets.top = self.navigationController.navigationBar.bounds.size.height;//让上边界的预留值和导航栏相等
        self.tableView.contentInset = insets;
        //把修改后的边界预留值赋值给tableView
        self.tableView.scrollIndicatorInsets = insets;//滚动指标点到滚动视图的边缘的距离
        
    }

    
}
//隐藏上下边栏
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (velocity.y > 0) {
        self.navigationController.navigationBar.hidden = YES;
        self.tabBarController.tabBar.hidden = YES;
    }else{
        self.navigationController.navigationBar.hidden = NO;
        self.tabBarController.tabBar.hidden = NO;
    }
}

//上拉加载
- (void)loadnewdata
{
    //加载小菊花
    [MBProgressHUDManager showHUDtoView:self.view string:@"努力加载中"];
    NSString *str = [NSString stringWithFormat:@"http://api.app.happyjuzi.com/v2.8.2/article/list/gif?net=wifi&page=%ld&pf=ios&res=320x568&size=20&uid=3931188994231555&ver=2.8.2", (long)self.number] ;
    [AfNetWorkingHandler GetWithStr:str complection:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        NSArray *arrTemp = [NSArray array];
        arrTemp = [GifModel modelHanderWithArray:arr];
        for (NSInteger i = 0; i < 20; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
        [MBProgressHUDManager removeHUD];
    }];
    [_tableView.mj_footer endRefreshing];
    self.number += 1;
}
//数据处理
-(void)dataHandler
{
    //加载小菊花
    [MBProgressHUDManager showHUDtoView:self.view string:@"努力加载中"];
    NSString *str = @"http://api.app.happyjuzi.com/v2.8.2/article/list/gif?net=wifi&page=1&pf=ios&res=320x568&size=20&uid=3931188994231555&ver=2.8.2";
    [AfNetWorkingHandler GetWithStr:str complection:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        self.dataArr = [GifModel modelHanderWithArray:arr];
        [_tableView reloadData];
        [MBProgressHUDManager removeHUD];
    }];
    [_tableView.mj_header endRefreshing];
}
//设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  300;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
//进入页面时更新
-(void)viewWillAppear:(BOOL)animated
{
    [_tableView reloadData];
}
//设置夜间模式的方法
-(void)setNightDayModel
{
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.tabBarController.tabBar.barStyle = UIBarStyleBlack;
}
-(void)setLightDayModel
{
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.tabBarController.tabBar.barStyle = UIBarStyleDefault;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GifCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellG"];
    //设置夜间模式
    [SetBlack setCellColorWithCell:cell];
    [SetBlack setLableTitleColorWithArr:@[cell.title]];
    //设置cell内容
    GifModel *m = _dataArr[indexPath.row];
    cell.str = m.thumb;
    cell.nameLable.text = m.name;
    cell.title.text = m.title;
    cell.width = m.width;
    cell.heigth = m.height;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
